package gameLogic;

import java.util.ArrayList;
import java.util.Iterator;

public class Strategy3InR extends Strategy {
	// 1 = get winning move
	// -1 = get blocking move
	private int type;
	private int targetStatus; // [observer status] to search for
	private BoardState targetSymbol;

	public Strategy3InR(Board b, BoardState s, Observer ob, int type) {
		super(b, s, ob);
		this.type = type;
		// if trying to win, search for player's possible 3inR
		// if trying to block, search for opponent's 3inR
		if ((type == 1 && side == BoardState.X)
				|| (type == -1 && side == BoardState.O)) {
			targetStatus = 111;
			targetSymbol = BoardState.X;
		} else {
			targetStatus = 222;
			targetSymbol = BoardState.O;
		}
		
		//System.out.println("Strategy: type="+type+" targetStatus="+targetStatus+" targetSymbol="+targetSymbol);
	}

	@Override
	public Move[] getAllMove() {
		ArrayList<Move> moveList = new ArrayList<Move>();
		Iterator<Move> i = getAllAllowed().iterator();
		while (i.hasNext()) {
			Move m = new Move(i.next());
			m.v = targetSymbol;
			if(ob.getMoveEffect(m, targetStatus) > 0)
			{
				m.v = side;
				moveList.add(m);
				
				//String action = (type==1) ? "Make" : "Block";
				//System.out.println(action+" 3InR! "+m);
				
				break;  // find only 1 solution
			}
		}
		return moveList.toArray(new Move[0]);
	}

	@Override
	protected int getPriority() {
		// winning has higher priority than blocking
		return (type == 1) ? 10 : 9;
	}
}
